home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format CD 52 / Amiga Format AFCD52 (Issue 136, May 2000).iso / -serious- / programming / other / dopus412-gpl / program / main19.c < prev    next >
C/C++ Source or Header  |  2000-02-28  |  15KB  |  617 lines

  1. /*
  2.  
  3. Directory Opus 4
  4. Original GPL release version 4.12
  5. Copyright 1993-2000 Jonathan Potter
  6.  
  7. This program is free software; you can redistribute it and/or
  8. modify it under the terms of the GNU General Public License
  9. as published by the Free Software Foundation; either version 2
  10. of the License, or (at your option) any later version.
  11.  
  12. This program is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15. GNU General Public License for more details.
  16.  
  17. You should have received a copy of the GNU General Public License
  18. along with this program; if not, write to the Free Software
  19. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  20.  
  21. All users of Directory Opus 4 (including versions distributed
  22. under the GPL) are entitled to upgrade to the latest version of
  23. Directory Opus version 5 at a reduced price. Please see
  24. http://www.gpsoft.com.au for more information.
  25.  
  26. The release of Directory Opus 4 under the GPL in NO WAY affects
  27. the existing commercial status of Directory Opus 5.
  28.  
  29. */
  30.  
  31. #include "DOpus.h"
  32.  
  33. void makereselect(winpar,win)
  34. struct DirWindowPars *winpar;
  35. int win;
  36. {
  37.     int a,num;
  38.     struct Directory *dir,*top;
  39.  
  40.     if (winpar->reselection_list) {
  41.         FreeMem(winpar->reselection_list,winpar->reselection_size);
  42.         winpar->reselection_list=NULL;
  43.     }
  44.     if (win==-1 ||
  45.         (dopus_curwin[win]->firstentry &&
  46.             dopus_curwin[win]->firstentry->type==ENTRY_CUSTOM)) return;
  47.  
  48.     winpar->reselection_dir=dopus_curwin[win];
  49.     winpar->reselection_win=win;
  50.     winpar->reselection_size=(dopus_curwin[win]->dirsel+
  51.         dopus_curwin[win]->filesel+1)*32;
  52.  
  53.     if (!(winpar->reselection_list=AllocMem(winpar->reselection_size,MEMF_CLEAR))) return;
  54.     dir=winpar->reselection_dir->firstentry;
  55.     a=num=0; top=NULL;
  56.     while (dir) {
  57.         if (dir->selected) {
  58.             CopyMem(dir->name,&winpar->reselection_list[a],32);
  59.             a+=32;
  60.         }
  61.         if (!top && num==dopus_curwin[win]->offset)
  62.             top=dir;
  63.         dir=dir->next;
  64.         ++num;
  65.     }
  66.     if (top) strcpy(winpar->top_name,top->name);
  67.     else winpar->top_name[0]=0;
  68.     winpar->offset=dopus_curwin[win]->offset;
  69.     winpar->hoffset=dopus_curwin[win]->hoffset;
  70. }
  71.  
  72. void doreselect(winpar,moveold)
  73. struct DirWindowPars *winpar;
  74. int moveold;
  75. {
  76.     int a,num=0,top=-1;
  77.     struct Directory *dir;
  78.  
  79.     if (winpar->reselection_list &&
  80.         winpar->reselection_dir==dopus_curwin[winpar->reselection_win]) {
  81.         dir=dopus_curwin[winpar->reselection_win]->firstentry;
  82.         while (dir) {
  83.             if (dir->selected) {
  84.                 dir->selected=0;
  85.                 updateselectinfo(dir,winpar->reselection_win,0);
  86.             }
  87.             if (top==-1 &&
  88.                 winpar->top_name[0] &&
  89.                 (LStrCmpI(dir->name,winpar->top_name))==0) top=num;
  90.             ++num;
  91.             dir=dir->next;
  92.         }
  93.         for (a=0;a<winpar->reselection_size;a+=32) {
  94.             if (winpar->reselection_list[a]) {
  95.                 dir=dopus_curwin[winpar->reselection_win]->firstentry;
  96.                 while (dir) {
  97.                     if (LStrCmpI(&winpar->reselection_list[a],dir->name)==0) {
  98.                         dir->selected=1;
  99.                         updateselectinfo(dir,winpar->reselection_win,0);
  100.                         break;
  101.                     }
  102.                     dir=dir->next;
  103.                 }
  104.             }
  105.         }
  106.         if (!moveold) {
  107.             if (top<0) top=winpar->offset;
  108.             dopus_curwin[winpar->reselection_win]->offset=top;
  109.             dopus_curwin[winpar->reselection_win]->hoffset=winpar->hoffset;
  110.         }
  111.         refreshwindow(winpar->reselection_win,1);
  112. /*
  113.         if (moveold && config->dynamicflags&UPDATE_SCROLL)
  114.             findfirstsel(winpar->reselection_win,-2);
  115. */
  116.         doselinfo(winpar->reselection_win);
  117.     }
  118. }
  119.  
  120. void shutthingsdown(louise)
  121. int louise;
  122. {
  123.     remclock();
  124.     if (louise!=1) {
  125.         main_proc->pr_WindowPtr=windowptr_save;
  126.         if (!status_iconified && status_publicscreen && Window) {
  127.             if (MainScreen) {
  128.                 config->scr_winx=Window->LeftEdge;
  129.                 config->scr_winy=Window->TopEdge;
  130.             }
  131.             else {
  132.                 config->wbwinx=Window->LeftEdge;
  133.                 config->wbwiny=Window->TopEdge;
  134.             }
  135.         }
  136.         closedisplay();
  137.     }
  138. }
  139.  
  140. void setupwindreq(wind)
  141. struct Window *wind;
  142. {
  143.     SetFont(wind->RPort,scr_font[FONT_GENERAL]);
  144.     SetAPen(wind->RPort,screen_pens[config->requestbg].pen);
  145.     RectFill(wind->RPort,
  146.         wind->BorderLeft+2,
  147.         wind->BorderTop+1,
  148.         wind->Width-wind->BorderRight-3,
  149.         wind->Height-wind->BorderBottom-2);
  150.     if (!(wind->Flags&WFLG_DRAGBAR))
  151.         do3dbox(wind->RPort,2,1,wind->Width-4,wind->Height-2);
  152.     SetDrawModes(wind->RPort,config->requestfg,config->requestbg,JAM2);
  153. }
  154.  
  155. void hilite_req_gadget(win,gadid)
  156. struct Window *win;
  157. USHORT gadid;
  158. {
  159.     struct Gadget *gad;
  160.  
  161.     gad=win->FirstGadget;
  162.     while (gad) {
  163.         if (gad->GadgetID==gadid && gad->GadgetType==GTYP_BOOLGADGET) {
  164.             SelectGadget(win,gad);
  165.             return;
  166.         }
  167.         gad=gad->NextGadget;
  168.     }
  169. }
  170.  
  171. simplerequest(char *txt,...)
  172. {
  173.     char *gads[11],*cancelgad=NULL,*gad;
  174.     int a,r,rets[10],num;
  175.     va_list ap;
  176.     struct DOpusSimpleRequest request;
  177.  
  178.     va_start(ap,txt); r=1; num=0;
  179.     for (a=0;a<10;a++) {
  180.         if (!(gad=(char *)va_arg(ap,char *))) break;
  181.         if (a==1) cancelgad=gad;
  182.         else {
  183.             gads[num]=gad;
  184.             rets[num++]=r++;
  185.         }
  186.     }
  187.     if (cancelgad) {
  188.         gads[num]=cancelgad;
  189.         rets[num]=0;
  190.         a=num+1;
  191.     }
  192.     for (;a<11;a++) gads[a]=NULL;
  193.  
  194.     request.strbuf=NULL;
  195.     request.flags=0;
  196.     return(dorequest(&request,txt,gads,rets,NULL));
  197. }
  198.  
  199. whatsit(txt,max,buffer,skiptxt)
  200. char *txt;
  201. int max;
  202. char *buffer,*skiptxt;
  203. {
  204.     char *gads[4];
  205.     int a=1,rets[3];
  206.     struct DOpusSimpleRequest request;
  207.  
  208.     request.strbuf=buffer;
  209.     request.strlen=max;
  210.     request.flags=0;
  211.  
  212.     gads[0]=str_okaystring; rets[0]=1;
  213.     if (skiptxt==(char *)-1) request.flags=SRF_GLASS|SRF_DIRGLASS;
  214.     else if (skiptxt==(char *)-2) request.flags=SRF_GLASS;
  215.     else if (skiptxt) {
  216.         rets[1]=2;
  217.         gads[a++]=skiptxt;
  218.     }
  219.     rets[a]=0;
  220.     gads[a++]=str_cancelstring;
  221.     for (;a<4;a++) gads[a]=NULL;
  222.  
  223.     return(dorequest(&request,txt,gads,rets,NULL));
  224. }
  225.  
  226. dorequest(request,txt,gads,rets,window)
  227. struct DOpusSimpleRequest *request;
  228. char *txt,**gads;
  229. int *rets;
  230. struct Window *window;
  231. {
  232.     int a;
  233.     struct Window *win=NULL;
  234.  
  235.     request->text=txt;
  236.     request->gads=gads;
  237.     request->rets=rets;
  238.  
  239.     if (window) {
  240.         win=window;
  241.         request->font=window->RPort->Font;
  242.     }
  243.     else if (reqoverride) {
  244.         request->font=scr_font[FONT_GENERAL];
  245.         request->hi=2;
  246.         request->lo=1;
  247.         request->fg=1;
  248.         request->bg=0;
  249.         win=reqoverride;
  250.     }
  251.     else {
  252.         if (!status_iconified || status_flags&STATUS_ISINBUTTONS) win=Window;
  253.         if (win) {
  254.             request->hi=screen_pens[config->gadgettopcol].pen;
  255.             request->lo=screen_pens[config->gadgetbotcol].pen;
  256.             request->fg=screen_pens[config->requestfg].pen;
  257.             request->bg=screen_pens[config->requestbg].pen;
  258.         }
  259.         else {
  260.             request->hi=-1;
  261.             request->lo=-1;
  262.             request->fg=-1;
  263.             request->bg=-1;
  264.         }
  265.         request->font=scr_font[FONT_REQUEST];
  266.     }
  267.     if (config->generalscreenflags&SCR_GENERAL_REQDRAG) {
  268.         request->flags|=SRF_BORDERS;
  269.         request->title=globstring[STR_DIRECTORY_OPUS_REQUEST];
  270.     }
  271.     else request->flags&=~SRF_BORDERS;
  272.  
  273.     request->flags|=SRF_RECESSHI|SRF_EXTEND;
  274.     request->value=(int)&requester_stringex;
  275.     fix_stringex(&requester_stringex);
  276.  
  277.     a=DoSimpleRequest(win,request);
  278.     return(((a==65535)?1:a));
  279. }
  280.  
  281. checkfiletypefunc(name,fn)
  282. char *name;
  283. int fn;
  284. {
  285.     struct dopusfiletype *type;
  286.     struct dopusfuncpar par;
  287.  
  288.     if ((type=checkfiletype(name,fn,1))) {
  289.         char title[256];
  290.  
  291.         par.which=type->which[fn]; par.stack=type->stack[fn];
  292.         par.key=par.qual=0;
  293.         par.pri=type->pri[fn]; par.delay=type->delay[fn];
  294.  
  295.         if (type->actionstring[fn][0]) {
  296.             do_title_string(type->actionstring[fn],title,0,BaseName(name));
  297.             dostatustext(title);
  298.         }
  299.         else title[0]=0;
  300.  
  301.         dofunctionstring(type->function[fn],BaseName(name),title,&par);
  302.         return(1);
  303.     }
  304.     return(0);
  305. }
  306.  
  307. struct dopusfiletype *checkfiletype(fullname,ftype,funconly)
  308. char *fullname;
  309. int ftype,funconly;
  310. {
  311.     struct dopusfiletype *type;
  312.     struct FileInfoBlock __aligned info;
  313.     int file;
  314.  
  315.     if (!(lockandexamine(fullname,&info))) return(NULL);
  316.     if (!(file=Open(fullname,MODE_OLDFILE))) return(NULL);
  317.  
  318.     type=dopus_firsttype;
  319.     while (type) {
  320.         if (status_haveaborted) break;
  321.         if (ftype==-2) {
  322.             if (type->iconpath && type->recognition &&
  323.                 (dochecktype(type,fullname,file,&info))) {
  324.                 Close(file);
  325.                 return(type);
  326.             }
  327.         }
  328.         else {
  329.             if (!funconly || (type->function[ftype] && type->function[ftype][0])) {
  330.                 if (type->recognition && (dochecktype(type,fullname,file,&info)) &&
  331.                     (ftype==-1 || (type->function[ftype] && type->function[ftype][0]))) {
  332.                     Close(file);
  333.                     return(type);
  334.                 }
  335.             }
  336.         }
  337.         type=type->next;
  338.     }
  339.     Close(file);
  340.     return(NULL);
  341. }
  342.  
  343. dochecktype(type,name,file,info)
  344. struct dopusfiletype *type;
  345. char *name;
  346. int file;
  347. struct FileInfoBlock *info;
  348. {
  349.     unsigned char buf[514],buf2[1024],*recog;
  350.     int
  351.         a,b,c,d,len,operation,fail,prot[2],tprot,equ,val,oldpos,
  352.         err,gotone=0,test;
  353.     struct DateStamp ds1,ds2;
  354.  
  355.     len=strlen((recog=type->recognition))+1; b=operation=0;
  356.  
  357.     Seek(file,0,OFFSET_BEGINNING);
  358.     for (a=0;a<len;a++) {
  359.         if (!operation) {
  360.             if (recog[a]>0 && recog[a]<FTYC_COMMANDOK)
  361.                 operation=recog[a];
  362.         }
  363.         else if (b==511 || recog[a]>FTYC_ENDLIMIT || !recog[a]) {
  364.             buf[b]=0;
  365.             fail=0; test=1;
  366.             switch (operation) {
  367.                 case FTYC_MATCH:
  368.                     if (!(checktypechars(file,buf))) fail=1;
  369.                     break;
  370.                 case FTYC_MATCHNAME:
  371.                     LParsePatternI(buf,buf2);
  372.                     if (!(LMatchPatternI(buf2,info->fib_FileName))) fail=1;
  373.                     break;
  374.                 case FTYC_MATCHBITS:
  375.                     getprotselvals(buf,prot);
  376.                     tprot=((~info->fib_Protection)&15)+(info->fib_Protection&~15);
  377.                     if (!((tprot&prot[0])==prot[0] && ((tprot&~prot[0])&prot[1])==0))
  378.                         fail=1;
  379.                     break;
  380.                 case FTYC_MATCHCOMMENT:
  381.                     LParsePatternI(buf,buf2);
  382.                     if (!(LMatchPatternI(buf2,info->fib_Comment))) fail=1;
  383.                     break;
  384.                 case FTYC_MATCHSIZE:
  385.                     d=strlen(buf); equ=2;
  386.                     for (c=0;c<d;c++) {
  387.                         if (equ==2 && !(isspace(buf[c]))) {
  388.                             if (buf[c]=='<') equ=-1;
  389.                             else if (buf[c]=='=') equ=0;
  390.                             else if (buf[c]=='>') equ=1;
  391.                             else {
  392.                                 fail=1;
  393.                                 break;
  394.                             }
  395.                         }
  396.                         else if (equ!=2 && isdigit(buf[c])) {
  397.                             val=atoi(&buf[c]);
  398.                             break;
  399.                         }
  400.                     }
  401.                     if (equ!=2) {
  402.                         if (equ==-1 && info->fib_Size>=val) fail=1;
  403.                         else if (equ==0 && info->fib_Size!=val) fail=1;
  404.                         else if (equ==1 && info->fib_Size<=val) fail=1;
  405.                     }
  406.                     break;
  407.                 case FTYC_MATCHDATE:
  408.                     getseldatestamps(buf,&ds1,&ds2);
  409.                     if (CompareDate(&(info->fib_Date),&ds1)<0 ||
  410.                         CompareDate(&ds2,&(info->fib_Date))<0) fail=1;
  411.                     break;
  412.                 case FTYC_MOVETO:
  413.                     test=0;
  414.                     if (buf[0]=='$') val=Atoh(&buf[1],-1);
  415.                     else val=atoi(buf);
  416.                     if (val==-1) err=Seek(file,0,OFFSET_END);
  417.                     else if (val>-1) err=Seek(file,val,OFFSET_BEGINNING);
  418.                     else err=-1;
  419.                     if (err==-1 || (system_version2==OSVER_37 && IoErr())) fail=1;
  420.                     break;
  421.                 case FTYC_MOVE:
  422.                     test=0;
  423.                     if (buf[0]=='$') val=Atoh(&buf[1],-1);
  424.                     else val=atoi(buf);
  425.                     if ((Seek(file,val,OFFSET_CURRENT))==-1 ||
  426.                         (system_version2==OSVER_37 && IoErr())) fail=1;
  427.                     if (err==-1) fail=1;
  428.                     break;
  429.                 case FTYC_SEARCHFOR:
  430.                     oldpos=Seek(file,0,OFFSET_CURRENT);
  431.                     if ((val=typesearch(file,buf,SEARCH_NOCASE|SEARCH_WILDCARD,NULL,0))==-1) {
  432.                         fail=1;
  433.                         Seek(file,oldpos,OFFSET_BEGINNING);
  434.                     }
  435.                     else Seek(file,val,OFFSET_BEGINNING);
  436.                     break;
  437.                 default:
  438.                     test=0;
  439.                     break;
  440.             }
  441.             if (!fail && test && recog[a]==FTYC_OR) gotone=1;
  442.             else if (fail) {
  443.                 while (recog[a]!=FTYC_OR && recog[a]!=FTYC_AND && recog[a]) ++a;
  444.                 if (recog[a]==FTYC_AND) break;
  445.             }
  446.             operation=b=0;
  447.         }
  448.         else buf[b++]=recog[a];
  449.     }
  450.     if (!fail || gotone) return(1);
  451.     return(0);
  452. }
  453.  
  454. checktypechars(file,match)
  455. int file;
  456. unsigned char *match;
  457. {
  458.     unsigned char matchbuf[258];
  459.     int len,clen,a,first=1,m,val,bpos;
  460.  
  461.     len=strlen(match);
  462.  
  463.     switch (match[0]) {
  464.         case '$':
  465.             clen=(len-1)/2;
  466.             break;
  467.         case '%':
  468.             clen=(len-1)/8;
  469.             break;
  470.         default:
  471.             clen=len; first=0;
  472.             break;
  473.     }
  474.     if (clen>256) clen=256;
  475.     if ((Read(file,matchbuf,clen))!=clen) return(0);
  476.     m=0; bpos=0;
  477.     switch (match[0]) {
  478.         case '$':
  479.             for (a=first;a<len;a+=2,m++) {
  480.                 if (match[a]!='?') {
  481.                     val=Atoh(&match[a],2);
  482.                     if (val!=matchbuf[m]) return(0);
  483.                 }
  484.             }
  485.             break;
  486.         case '%':
  487.             for (a=first;a<len;a++) {
  488.                 if (match[a]!='?') {
  489.                     if (match[a]=='1' && !(matchbuf[m]&(1<<bpos))) return(0);
  490.                     else if (match[a]=='0' && (matchbuf[m]&(1<<bpos))) return(0);
  491.                 }
  492.                 if ((++bpos)==8) {
  493.                     bpos=0; ++m;
  494.                 }
  495.             }
  496.             break;
  497.         default:
  498.             for (a=first;a<len;a++,m++) {
  499.                 if (match[a]!='?' && match[a]!=matchbuf[m]) return(0);
  500.             }
  501.             break;
  502.     }
  503.     return(1);
  504. }
  505.  
  506. typesearch(file,find,flags,buffer,bufsize)
  507. int file;
  508. char *find;
  509. int flags;
  510. char *buffer;
  511. int bufsize;
  512. {
  513.     unsigned char *findbuf,matchbuf[256];
  514.     int matchsize,a,len,size,oldpos;
  515.  
  516.     len=strlen(find);
  517.     if (find[0]=='$') {
  518.         for (a=1,matchsize=0;a<len;a+=2,matchsize++) {
  519.             if (find[a]=='?') matchbuf[matchsize]='?';
  520.             else matchbuf[matchsize]=Atoh(&find[a],2);
  521.         }
  522.         flags&=~(SEARCH_NOCASE|SEARCH_ONLYWORDS);
  523.     }
  524.     else {
  525.         for (a=0,matchsize=0;a<len;a++) {
  526.             if (find[a]=='\\') {
  527.                 if (find[a+1]=='\\') {
  528.                     matchbuf[matchsize++]='\\';
  529.                     ++a;
  530.                 }
  531.                 else {
  532.                     matchbuf[matchsize++]=atoi(&find[a+1]);
  533.                     a+=3;
  534.                 }
  535.             }
  536.             else if (flags&SEARCH_NOCASE) matchbuf[matchsize++]=toupper(find[a]);
  537.             else matchbuf[matchsize++]=find[a];
  538.         }
  539.     }
  540.  
  541.     search_found_lines=search_last_line_pos=0;
  542.     if (buffer) return(searchbuffer(buffer,bufsize,matchbuf,matchsize,flags));
  543.     else {
  544.         if (!(findbuf=AllocMem(32004,MEMF_CLEAR))) return(-1);
  545.         FOREVER {
  546.             if (status_haveaborted) {
  547.                 myabort();
  548.                 break;
  549.             }
  550.             oldpos=Seek(file,0,OFFSET_CURRENT);
  551.             if ((size=Read(file,findbuf,32000))<1) break;
  552.             if ((searchbuffer(findbuf,size,matchbuf,matchsize,flags))==1) {
  553.                 oldpos+=(search_found_position-findbuf);
  554.                 FreeMem(findbuf,32004);
  555.                 return(oldpos);
  556.             }
  557.             if (status_haveaborted) continue;
  558.             if (size<32000) break;
  559.             Seek(file,-matchsize,OFFSET_CURRENT);
  560.         }
  561.         FreeMem(findbuf,32004);
  562.     }
  563.     return(-1);
  564. }
  565.  
  566. searchbuffer(findbuf,size,matchbuf,matchsize,flags)
  567. char *findbuf;
  568. int size;
  569. char *matchbuf;
  570. int matchsize,flags;
  571. {
  572.     unsigned char lastchar,mchar;
  573.     int a,matchchar,matchstart,lastlines,lastpos;
  574.  
  575.     matchchar=matchstart=lastchar=0;
  576.     lastlines=search_found_lines; lastpos=search_last_line_pos;
  577.  
  578.     for (a=0;a<size;a++) {
  579.         if (status_haveaborted) return(-1);
  580.         if ((mchar=(flags&SEARCH_NOCASE)?toupper(findbuf[a]):findbuf[a])==10) {
  581.             ++search_found_lines;
  582.             search_last_line_pos=a+1;
  583.         }
  584.         if ((!(flags&SEARCH_WILDCARD) || matchbuf[matchchar]!='?') &&
  585.             matchbuf[matchchar]!=mchar) {
  586.             if (matchchar>0) {
  587.                 a=matchstart;
  588.                 search_found_lines=lastlines;
  589.                 search_last_line_pos=lastpos;
  590.                 matchchar=matchstart=0;
  591.             }
  592.         }
  593.         else {
  594.             if (!(flags&SEARCH_ONLYWORDS) || matchchar || isonlyword(lastchar)) {
  595.                 if (matchchar==0) {
  596.                     matchstart=a;
  597.                     lastlines=search_found_lines;
  598.                     lastpos=search_last_line_pos;
  599.                 }
  600.                 if ((++matchchar)==matchsize) {
  601.                     if (!(flags&SEARCH_ONLYWORDS) || isonlyword(findbuf[a+1])) {
  602.                         search_found_position=findbuf+matchstart;
  603.                         search_found_size=matchchar;
  604.                         return(1);
  605.                     }
  606.                     a=matchstart;
  607.                     search_found_lines=lastlines;
  608.                     search_last_line_pos=lastpos;
  609.                     matchchar=matchstart=0;
  610.                 }
  611.             }
  612.         }
  613.         lastchar=mchar;
  614.     }
  615.     return(-1);
  616. }
  617.